﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;

namespace BLL
{
    public static class ques_BLL
    {
        //创建数据库连接
        private static dbExamDataContext db = new dbExamDataContext();

        /// <summary>
        /// 获取试题
        /// </summary>
        /// <param name="examNum">试卷编号</param>
        public static object GetQuesSource(string examNum)
        {
            object obj = new object();
            obj = from p in db.STK orderby p.txNum,p.stTM where p.TX.examNum == decimal.Parse(examNum) select new { p.stNum, p.stTM, p.seleA, p.seleB, p.seleC, p.seleD,p.TX.txName,p.TX.fullScore,p.TX.singleScore,p.TX.tl,p.TX.timeArea, p.analysis,p.result };
            return obj;
        }

        /// <summary>
        /// 添加新的试题
        /// </summary>
        /// <param name="newSTK"></param>
        /// <returns></returns>
        public static bool AddQues(STK newSTK)
        {
            bool bl = false;
            try
            {
                //验证该题型下是否有与新题重名的试题
                if (db.STK.Single(p => p.stTM == newSTK.stTM && p.txNum == newSTK.txNum).stNum > -1)
                    return false;
            }
            catch { } 
            //准备数据
            db.STK.InsertOnSubmit(newSTK);
            //执行更新
            db.SubmitChanges();
            bl = true;
            return bl;
        }

        /// <summary>
        /// 更新试题
        /// </summary>
        /// <param name="stNum">要更新的试题编号 </param>
        /// <param name="newSTK">新的试题数据对象</param>
        public static bool UpdateQues(decimal stNum, STK newSTK)
        {
            bool bl = false;
            try
            {
                //找到需要更新的数据
                STK stk = db.STK.Single(p => p.stNum == stNum);
                //准备数据
                stk.stTM = newSTK.stTM;
                stk.txNum = newSTK.txNum;
                stk.seleD = newSTK.seleD;
                stk.seleC = newSTK.seleC;
                stk.seleB = newSTK.seleB;
                stk.seleA = newSTK.seleA;
                stk.result = newSTK.result;
                stk.analysis = newSTK.analysis;
                //执行更新
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }

        /// <summary>
        /// 删除试题
        /// </summary>
        /// <param name="stNum">要删除的试题编号</param>
        public static bool DeleteQues(decimal stNum)
        {
            db = new dbExamDataContext();
            bool bl = false;
            try
            {
                STK st = db.STK.Single(p => p.stNum == stNum);
                db.STK.DeleteOnSubmit(st);
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }
    }
}
